#coding:utf-8
from django.contrib import admin
from django.contrib.admin import SimpleListFilter
from django.contrib.auth.models import Group
from django.utils.translation import ugettext_lazy as _
from lovewith.account.models import MtUser


class UserListFilter(SimpleListFilter):
    title = _(u'其它')
    parameter_name = 'decade'

    def lookups(self, request, model_admin):
        return (
            ('review', _('待审核商家')), ('actived', _('登录过的用户')),
        )

    def queryset(self, request, queryset):
        if self.value() == 'review':
            return queryset.filter(group=1, full_name__exact=True)
        if self.value() == 'actived':
            return queryset.exclude(access_token='')


class MtUserAdmin(admin.ModelAdmin):
    list_display = (
        'id', 'username', 'email', 'group', 'full_name', 'email', 'phone', 'qq', 'is_admin', 'joindate', 'lastlogin')
    #添加快速查询栏 
    search_fields = ('username',)
    #过滤器 
    list_filter = ('group', UserListFilter)
    actions = ['']
    #只读字段
    readonly_fields = ('username', 'password', 'avatar', 'join_date', 'last_login')

    def lastlogin(self, obj):
        return obj.last_login.strftime('%Y/%m/%d %H:%M:%S')

    lastlogin.short_description = '最后登录时间'

    def joindate(self, obj):
        return obj.join_date.strftime('%Y/%m/%d %H:%M:%S')

    joindate.short_description = '加入时间'


#注册显示model
admin.site.register(MtUser, MtUserAdmin)
admin.site.unregister(Group)